package com.julang.check.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.julang.check.api.entity.YyHtmlReport;
import com.julang.check.api.mapper.YyHtmlReportMapper;
import com.julang.check.api.service.YyHtmlReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.julang.check.utils.CommonData;
import com.julang.check.utils.SingnalData;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.julang.check.utils.CommonData.DATEFORMATTER;
import static com.julang.check.utils.MessageInfo.FILENOTEXIST;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵自强
 * @since 2019-06-05
 */
@Service
public class YyHtmlReportServiceImpl extends ServiceImpl<YyHtmlReportMapper, YyHtmlReport> implements YyHtmlReportService {
    /**
     * 文件解析完成之后，更新文献表的状态
     * */
    public void updateHtmlStatusAfterAnalysis(YyHtmlReport report) {
        report.setFileStatus(1);
        report.setAnalysisTime(LocalDateTime.now());
        this.updateById(report);
    }
    /**
     * 获取html重复字数 重复率 文档名 总字数  并且更新记录数据到数据库
     * */
    public YyHtmlReport getHtmlFileData(int userId, String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            System.out.println(FILENOTEXIST);
            return null;
        }
        //解析html文件获取重复字数 重复率 总字数 文档题目
        Document document = null;
        try {
            document = Jsoup.parse(new File(fileName), "UTF-8");
        } catch (IOException e) {
            return null;
        }
        Elements datas = document.getElementsByClass("ReportResult_Con1");
        String toatlPercent = StringUtils.substringAfter(
                datas.get(0).children().get(0).children().get(0).children().get(1).ownText(), "总文字复制比：");
        String repeatCount = datas.get(0).children().get(1).children().get(0).children().get(0).children().get(0).ownText();
        String wordCount = datas.get(0).children().get(1).children().get(0).children().get(0).children().get(1).ownText();
        Elements nameElements = document.getElementsByClass("TextInfoContent_left").select("ul");
        String wordName = nameElements.get(0).children().get(0).ownText();
        YyHtmlReport reductionReportHtml = new YyHtmlReport(userId, fileName);
        reductionReportHtml.setTotalWordCount(StringUtils.substringBefore(wordCount, "（不含参考文献）"));
        reductionReportHtml.setRepeatPercent(toatlPercent);
        reductionReportHtml.setWordName(wordName);
        reductionReportHtml.setUploadTime(LocalDateTime.now());
        reductionReportHtml.setRepeatWordCount(repeatCount);
        //保存到数据库
        System.out.println(reductionReportHtml.toString());
        return reductionReportHtml;
    }

    /**是否存在相同重复率和文档名字的记录 不存在返回false*/
    public boolean isRecordExists(String wordName, String repeatPercent) {
        System.out.println("查找重复率：" + wordName + "  查找文档名："  + repeatPercent);
        YyHtmlReport yyHtmlReport = this.getOne(new QueryWrapper<YyHtmlReport>().lambda()
                .eq(YyHtmlReport::getWordName, wordName)
                .eq(YyHtmlReport::getRepeatPercent, repeatPercent)
                .last("limit 1"));
        return yyHtmlReport != null;
    }
}
